package com.fasteat.menu.controller;


import com.alibaba.fastjson.JSON;
import com.fasteat.common.entity.Result;
import com.fasteat.common.util.FileUtil;
import com.fasteat.menu.entity.Menu;
import com.fasteat.menu.entity.vo.MenuSearchVo;
import com.fasteat.menu.entity.vo.MenuMessageVo;
import com.fasteat.menu.entity.client.MenuSearchMap;
import com.fasteat.menu.service.MenuService;
import com.fasteat.menu.util.UpdateProxy;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author run
 * @since 2021-01-19
 */
@Api(tags = "菜谱相关接口")
@CrossOrigin
@RestController
@RequestMapping("/fasteat/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private UpdateProxy updateProxy;

    @PostMapping("/")
    @ApiOperation("发布菜谱")
    public Result createMenu(@Valid @RequestBody @ApiParam (name = "菜谱对象",required = true,value = "menu") Menu menu){
        return menuService.createMenu(menu) ? Result.success("发布成功") : Result.error("发布失败");
    }

    @PostMapping("/upload")
    @ApiOperation("上传图片接口")
    public Result uploadImg(@RequestBody MultipartFile file){
        Map<String,Object> result = (Map<String, Object>) JSON.parse(FileUtil.upload(file));
        return Result.success().data((String)result.get("domain") + result.get("path"));
    }

    @PostMapping("/{id}")
    @ApiOperation("通过菜谱id获取菜谱信息")
    public Result searchMenuById(@PathVariable(value = "id") String menuId
                                    ,@RequestBody(required = false) @ApiParam(required = false,value = "userId") String userId){
        return Result.success().data(menuService.searchMenuById(menuId,userId));
    }

    @GetMapping("/userMenu/{id}/{page}/{size}")
    @ApiOperation("通过用户id分页获取其创作的菜谱")
    public Result searchMenuOfUser(@PathVariable(value = "id") String userId
                                    , @PathVariable(value = "page") Integer page
                                    , @PathVariable(value = "size") Integer size){
        return Result.success().data(menuService.searchMenuOfUser(userId,page,size));
    }

    @PutMapping("/")
    @ApiOperation("修改菜谱")
    public Result updateMenu(@RequestBody @ApiParam(name = "菜谱对象",required = true,value = "menu") Menu menu){
        return ((MenuService)(updateProxy.bind(menuService).getProxyInstance())).updateMenu(menu)
                ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除菜谱")
    public Result deleteMenu(@PathVariable(value = "id") String menuId){
        return menuService.deleteMenu(menuId)
                ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("/{keyword}/{page}/{size}")
    @ApiOperation("通过关键词分页获取菜谱")
    public Result searchMenus(@PathVariable(value = "keyword") String keyword
                                , @PathVariable(value = "page") Integer page
                                , @PathVariable(value = "size") Integer size){
        return Result.success().data(menuService.searchMenus(keyword, page, size));
    }


    @GetMapping("/")
    @ApiOperation("获取全部菜谱")
    public Result searchMenus(){
        return Result.success().data(menuService.searchMenus());
    }

    @PutMapping("/{keyword}/{page}/{size}")
    @ApiOperation("根据关键词分页高亮搜索菜谱列表(用户接口)")
    public Result searchMenuOfKeyword(@PathVariable(value = "keyword") String keyword
                                        , @PathVariable(value = "page") Integer page
                                        , @PathVariable(value = "size") Integer size
                                        , @RequestBody(required = false) @ApiParam(value = "searchMap") MenuSearchMap searchMap){
        return Result.success().data(menuService.searchMenuOfKeyWord(keyword,page,size,searchMap));
    }

    @PutMapping("/getByMaterial/{page}/{size}")
    @ApiOperation("根据食材分页高亮搜索菜谱列表（用户接口）")
    public Result searchMenuOfMaterial(@RequestBody @ApiParam(value = "menuSearchVo")MenuSearchVo menuSearchVo
                                        , @PathVariable(value = "page") Integer page
                                        , @PathVariable(value = "size") Integer size){
        return Result.success().data(menuService.searchMenuOfMaterial(menuSearchVo,page,size));
    }

    @PostMapping("/history")
    @ApiOperation("根据菜谱id集合获取对应的标题封面（远程调用）")
    public Result searchSimpleMenuOfIds(@RequestBody List<String> menuIds){
        return Result.success().data(menuService.searchSimpleMenuOfIds(menuIds));
    }

    @PostMapping("/update")
    @ApiOperation("刷新菜谱点赞、浏览、评论、收藏数据（远程调用接口）")
    public void updateMenuMessage(@RequestBody List<MenuMessageVo> menuMessageVos){
        menuService.updateMenuMessage(menuMessageVos);
    }

    @GetMapping("/category")
    @ApiOperation("获取菜谱分类列表")
    public Result getMenuCategory(){
        return Result.success().data(menuService.getMenuCategory());
    }

    @GetMapping("/userPraise/{id}")
    @ApiOperation("获取用户被点赞的次数")
    public Result getUserPraise(@PathVariable(value = "id") String userId){
        return Result.success().data(menuService.getUserPraise(userId));
    }

    @GetMapping("/menuMessage/{id}")
    @ApiOperation("获取菜谱的点赞、评论、收藏、评论信息")
    public Result getMenuMessage(@PathVariable(value = "id") String menuId){
        return Result.success().data(menuService.getMenuMessage(menuId));
    }
}

